//
//  XOWMHelper.m
//  XOWMSDK
//
//  Created by skyline on 2018/7/25.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "XOWMHelper.h"
#import "TCCIAPModel.h"

@interface XOWMHelper()
{
    NSMutableDictionary *_localInfo;
    NSMutableArray *_accountArr;
    NSString *_account;
    BOOL _isBind;
    BOOL _isAuthenticate;
}
@end

@implementation XOWMHelper
static XOWMHelper*_helper;
static dispatch_once_t onceToken;
+ (instancetype)shareHelp
{
    if (!_helper)
    {
        dispatch_once(&onceToken,^{
            _helper = [[XOWMHelper alloc]init];
        });
    }
    return  _helper;
}

- (NSDictionary *)localInfo
{
    if (!_localInfo)
    {
        _localInfo = [NSMutableDictionary dictionary];
        [_localInfo setValue:[CCDevice UUID] forKey:@"imei"];//设备号<设备唯一标识符>
        [_localInfo setValue:[CCIP IP].wanIP forKey:@"ip"];//设备公网IP//获取公网IP比较费时，做异步处理
        [_localInfo setValue:[[XOWMHelper shareHelp] appID] forKey:@"appid"];//appid<暂定1>
        [_localInfo setValue:@"1"forKey:@"terminal"];//设备平台<苹果设备为1>
        [_localInfo setValue:@"" forKey:@"phone"];//添加手机字段
    }
    return _localInfo;
}

- (NSMutableArray *)accountArr
{
    if (!_accountArr)
    {
        _accountArr =  [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:SKLTokenKey];
//        _accountArr = [NSMutableArray arrayWithArray:[CCCache valueFromKey:SKLTokenKey]];//直接操作accountArr增删动作，自动存储，不需要再调用存储操作
    }
    return _accountArr;
}

- (NSString *)account
{
    return _account;
}

- (NSString *)accountID
{
    return [[XOWMPlatform platform] accountID]?[[XOWMPlatform platform] accountID]:@"";
}

- (NSString *)appID
{
    return [[XOWMPlatform platform] appID];
}
- (NSString *)country
{
    return [CCIP IP].wanCountry;
}
- (NSString *)city
{
    return [CCIP IP].wanCity;
}
- (NSString *)area
{
    return [CCIP IP].wanRegion;
}
- (BOOL)isBind
{
    return _isBind;
}

- (BOOL)isAuthenticate
{
    return _isAuthenticate;
}

+ (void)yjd_insertAccount:(NSDictionary *)dict
{
    _helper = [XOWMHelper shareHelp];
    
    _helper->_account = [dict valueForKey:@"name"];
    NSString *password = [dict valueForKey:@"password"];
    if (_helper->_account && password)
        [_helper.accountArr yjd_enumerate:@{_helper->_account:password}];
}

+ (void)yjd_setupBindState:(NSString *)string
{
    _helper = [XOWMHelper shareHelp];
    
    _helper->_isBind = ([string length] > 1)?YES:NO;
}

+ (void)yjd_setupAuthenticate:(NSString *)string
{
    _helper = [XOWMHelper shareHelp];
    
    _helper->_isAuthenticate = ([string intValue] == 1)?YES:NO;
}



- (void)sp_getLoginState {
    NSLog(@"Get Info Success");
}
@end
